/*
 * viewer.cpp
 *
 *  Created on: Apr 30, 2011
 *      Author: tqlong
 */

#include <QtGui/QWidget>
#include <QtGui/QPen>
#include <QtCore/QPointF>
#include <QtCore/QSize>
#include <cassert>
#include "viewer.h"

Viewer::Viewer(const std::string& fName, const std::string& format) :
  m_fName(fName), m_format(format), m_set(fName, format, false)
{
  m_set.reload();
//  RaDecDataset radec("ra_dec.bin");
//  radec.reload();
}

void Viewer::plot(Plotter& pl)
{
  QPen pen(Qt::SolidLine);
  pen.setWidth(2);
  pen.setColor(Qt::blue);
  common::drawDSet(m_set, pl,  pen);
}

void Viewer::reload()
{
  m_set.reload();
}

// ============ AXIS point converters ===============

QPointF Axis::point(double x, double y) {
  QSize size = m_widget->size();
  x = (x-m_xmin)/(m_xmax-m_xmin) * size.width();
  y = (m_ymax-y)/(m_ymax-m_ymin) * size.height();
  //Log::m_log << "w=" << size.width() << " h=" << size.height() << " x=" << x << " y=" << y << endl;
  return QPointF(x, y);
}

QPointF Axis::point(const arma::vec& v) {
  assert(v.n_elem > 0);
  if (v.n_elem >= 2)
    return point(v[0], v[1]);
  else
    return point(v[0], 0);
}

QPointF Axis::distance(double x, double y)
{
  QSize size = m_widget->size();
  x = x/(m_xmax-m_xmin) * size.width();
  y = y/(m_ymax-m_ymin) * size.height();
  return QPointF(x,y);
}

QPointF Axis::reversePoint(const QPointF& p)
{
  QSize size = m_widget->size();
  double x = p.x(), y = p.y();
  x = m_xmin + x / size.width() * (m_xmax-m_xmin) ;
  y = m_ymax - y / size.height() * (m_ymax-m_ymin);
  return QPointF(x,y);
}

// =================== common::drawVVec =====================

void
common::drawVVec(const std::vector<arma::vec>& vvec, Plotter& pl, const QPen& pen)
{
  pl.setPen(pen);
  int n = vvec.size();
  for (int i = 0; i < n; i++)
    {
      const arma::vec& p = vvec[i];
      pl.drawEllipse(pl.point(p), 2.0, 2.0);
    }
}
